﻿using Lidgren.Network;
using Paintball.Network;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace Paintball.Abstraction
{
    public sealed class PaintballServer : ServerAgent
    {
        //Fields
        private Action<string> Feedback;
        private GameState State { get; set; }

        private Dictionary<NetConnection, Player> KnownPlayerData;

        //ctor
        public PaintballServer(Action<string> feedback)
        {
            Feedback = feedback;
        }

        //Methods
        public override void InitalizeServer()
        {
            State = GameState.Lobby;
            KnownPlayerData = new Dictionary<NetConnection, Player>();


            Feedback(string.Format("Now Hosting on {0}", Server.Configuration.BroadcastAddress));
            ThreadPool.QueueUserWorkItem(o =>
                {
                    while (Alive)
                    {
                        DateTime now = DateTime.Now;
                        //Switch the State of the Game
                        switch (State)
                        {
                            case GameState.Lobby:
                                LobbyUpdate();
                                break;

                            case GameState.Game:
                                GameUpdate();
                                break;

                            case GameState.PostGame:
                                PostGameUpdate();
                                break;
                        }
                        //Send out messages to all Clients
                        TimeSpan interval = DateTime.Now - now;
                        if(interval > TimeSpan.FromSeconds(0.5f))
                            Feedback(string.Format("Update loop Complete in {0}ms", interval));
                    }
                });

        }

        private void PostGameUpdate()
        {

        }
        private void GameUpdate()
        {

        }
        private void LobbyUpdate()
        {

        }

        protected override void CreateConfig(NetPeerConfiguration config)
        {
            config.Port = SafePort;
        }

        protected override void HandleMessage(NetIncomingMessageType type, NetIncomingMessage msg)
        {
            switch (type)
            {
                case NetIncomingMessageType.DiscoveryRequest:
                    {
                        DefaultDiscoveryRequestHandler(msg); 
                        Feedback(string.Format("Discovery Request from {0}", msg.SenderEndPoint));
                        break;
                    }

                case NetIncomingMessageType.StatusChanged:
                    {
                        NetConnectionStatus status = (NetConnectionStatus)msg.ReadByte();
                        if (status == NetConnectionStatus.Connected)
                        {
                            //A Client has connected
                        }
                        break;
                    }

                default:
                    break;
            }
        }
        protected override void HandleUnhandledDataMessage(NetIncomingMessage msg)
        {

        }
        
    }
}
